import api from "../api";
import store from '../store'


export const login = () => {
    return new Promise((resolve, reject) => {
        uni.login({
            provider: 'weixin',
            success: (res) => {
                console.log("微信登录uni.login", "success", res);
                resolve(res)
            },
            fail: (err) => {
                console.log("微信登录uni.login", "error", err);
                reject(err)
            }
        });
    }).then(res => {
        return api.wxLogin({
            code: res.code
        })
    }).then(res => {
        console.log("微信登录 store", "success", res);
        store.commit("user/setAccessToken", res.jwt)
        if (res.author.username.startsWith('临时用户')) {
            store.commit("user/setTmpUserinfo", res.author)
        } else {
            store.commit("user/setTmpUserinfo", {username: '临时用户'})
        }
        if (store.getters['user/hasUserinfo']) {
            store.commit("user/setUserinfo", res.author)
        }
        console.log(store.getters['user/getAccessToken'])
    })
}




